﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using QLTT.Models;
using QLTT.Commands;
using System.Windows.Input;

namespace QLTT.ViewModels
{
    class ScheduleViewModelCollection
    {
        private List<ScheduleViewModel> _lstSchedule;
        public ScheduleViewModelCollection(ClassViewModel cVM,ClassTimeViewModel ctVM, icanDataContext dataContext)
        {
            _lstSchedule = new List<ScheduleViewModel>();
            for (short k = 0; k < 7; k++)
            {
                ScheduleViewModel oSVM = new ScheduleViewModel(cVM, ctVM, k, dataContext);

                foreach (ScheduleViewModel sVM in cVM.Schedules)
                {
                    if (sVM.GetHashCode() == oSVM.GetHashCode())
                    {
                        oSVM.Teacher = sVM.Teacher;
                        break;
                    }
                }
                _lstSchedule.Add(oSVM);
            }
        }
        public List<ScheduleViewModel> Items
        {
            get
            {
                return _lstSchedule;
            }
        }
    }
    class AllScheduleViewModel:WorkspaceViewModel
    {
        private List<ScheduleViewModelCollection> _lstScheduleViewModel;
        private icanDataContext _dataContext;
        private DelegateCommand _saveCommand;
        private DelegateCommand _cancelCommand;
        private ClassViewModel _cVM;
        public AllScheduleViewModel(ClassViewModel classVM, icanDataContext dataContext)
        {
            _dataContext = dataContext;
            _cVM = classVM;
            List<ClassTime> lstClassTime = _dataContext.ClassTimes.ToList();
           

            _lstScheduleViewModel = new List<ScheduleViewModelCollection>();
            for (int i = 0; i < lstClassTime.Count; i++)
            {
                ClassTimeViewModel ctVM = new ClassTimeViewModel(_dataContext, lstClassTime[i]);
                ScheduleViewModelCollection lstSVM = new ScheduleViewModelCollection(classVM, ctVM, _dataContext);//new List<ScheduleViewModel>();
                _lstScheduleViewModel.Add(lstSVM);
            }
            

        }
                
        public List<ScheduleViewModelCollection> AllSchedules
        {
            get { return _lstScheduleViewModel; }
            set {
                _lstScheduleViewModel = value;
                base.OnPropertyChanged("AllSchedules");
            }
        }
      
        public ICommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    _saveCommand = new DelegateCommand(SaveSchedule);
                }
                return _saveCommand;
            }
        }
        public ICommand CancelCommand
        {
            get
            {
                if (_cancelCommand == null)
                {
                    _cancelCommand = new DelegateCommand(Cancel);
                }
                return _cancelCommand;
            }
        }
        private void Cancel()
        {
            Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceRemove, this);
        }
        private void SaveSchedule()
        {
            List<ScheduleViewModel> lstSVMs = new List<ScheduleViewModel>();
            foreach (ScheduleViewModelCollection sVMCollection in _lstScheduleViewModel)
            {
                List<ScheduleViewModel> lstSVM = sVMCollection.Items.Where(s => s.Teacher > 0).ToList();
                if (lstSVM != null && lstSVM.Count > 0)
                    lstSVMs.AddRange(lstSVM);
            }
            List<ScheduleViewModel> sVMs = _cVM.Schedules.Where(s => lstSVMs.FindIndex(s1 => s1.GetUniqueCode() == s.GetUniqueCode()) < 0).ToList();
            foreach (ScheduleViewModel sVM in sVMs)
            {
                _cVM.Schedules.Remove(sVM);
                if(sVM.ID > 0) //Update
                    sVM.Delete();
            }
            sVMs = lstSVMs.Where(s => _cVM.Schedules.FindIndex(s1 => s1.GetUniqueCode() == s.GetUniqueCode()) < 0).ToList();
            foreach (ScheduleViewModel sVM in sVMs)
            {
                if(_cVM.ID>0) //update
                    sVM.Save();
                _cVM.Schedules.Add(sVM);
            }

            Mediator.NotifyColleagues<List<ScheduleViewModel>>(Messages.ListScheduleUpdated, lstSVMs);
            Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceRemove, this);
        }
                
        
    }
}
